<?
  /* File:    conversion.php: a collection of data conversion functions
   * Type:    CMS function library
   * Author:  udo.schroeter@gmail.com
   * License: LGPL / commercially licensed 
   * Todo:    /
   * Changes: -
   */
  $config['site']['dateformat'] = getDefault(
    $config['site']['dateformat'], 'H:i d.m.Y');

  function stringListToArray($txt)
  {
    $result = array();
    $lines = explode("\n", $txt);
    if (sizeof($lines)>0)
      foreach ($lines as $line)
      {
        $line = trim($line);
        $k = CutSegment('=',$line);
        $result[$k] = $line;
      }
    return($result);
  }

  function arrayToStringList($array)
  {
    $result = '';
    if (is_array($array))
      foreach ($array as $k => $v)
      {
        $result .= $k.'='.$v."\n";
      }
    return($result);
  }

  function DissectTimeStamp($raw)
  {
    return
      substr($raw, 6, 2).'.'.  // day
      substr($raw, 4, 2).'.'.  // month
      substr($raw, 0, 4).' '.  // year
      substr($raw, 8, 2).':'.  // hour
      substr($raw, 10, 2); // minutee
  }
  
  function timestampToDate($raw)
  {
    // this compensates for a change in MySQL formatting the timestamp
    if (substr($raw, 4, 1) == '-')
    {
      $day =  substr($raw, 7, 2);
      $month = substr($raw, 5, 2);
      $year = substr($raw, 0, 4);
      $hour = substr($raw, 11, 2);
      $minute = substr($raw, 14, 2); 
      $second = substr($raw, 17, 2);
    }
    else
    {
      $day =  substr($raw, 6, 2);
      $month = substr($raw, 4, 2);
      $year = substr($raw, 0, 4);
      $hour = substr($raw, 8, 2);
      $minute = substr($raw, 10, 2); 
      $second = substr($raw, 12, 2);
    }
    return(mktime($hour, $minute, $second, $month, $day, $year)); 
  }
  
  function ageToString($unixDate)
  {
    $result = '';
    $oneMinute = 60;
    $oneHour = $oneMinute*60;
    $oneDay = $oneHour*24;
    $difference = time() - $unixDate;
     // todo: not implemented! 
    if ($difference < $oneMinute)
      $result = 'new';
    else if ($difference < $oneHour)
      $result = round($difference/$oneMinute).' min. ago';
    else if ($difference < $oneDay)
      $result = date('H:i', $unixDate);
    else
      $result = dateToString($unixDate);
    return($result);
  }
  
  function dateToTimestamp($unixDate)
  {
    return(date('YmdHis', $unixDate));
  }
  
  function dateToString($unixDate)
  {
    global $config;
    return(date($config['site']['dateformat'], $unixDate));
  }
  
  function timeToString($unixDate)
  {
    global $config;
    return(date($config['site']['timeformat'], $unixDate));
  }
  
  function dateTimeToString($unixDate)
  {
    global $config;
    return(date($config['site']['dateformat'].' '.$config['site']['timeformat'], $unixDate));
  }
  
  function stringToDateTime($string, $formatCode = null)
  {
    global $config;
    // list of allowed placeholders
    $placeHolders = array('Y', 'm', 'd', 'H', 'i', 's', 'j', 'y', 'n');
    // the meanings of those placeholders
    $placeHoldersMeanings = array('year', 'month', 'day', 'hour', 'minute', 'second', 'day', 'year', 'month');
    // if not formatting code is given, assume standard date + time
    if ($formatCode == null)
      $formatCode = $config['site']['dateformat'].' '.$config['site']['timeformat'];
    // determine the order of the placeholders used in the formatting string
    for ($a = 0; $a < strlen($formatCode); $a++) 
    {
      $phPositionFound = array_search(substr($formatCode, $a, 1), $placeHolders);
      if (!($phPositionFound === false))
        $phOrder[] = $placeHoldersMeanings[$phPositionFound];
    }
    // prepare the mask for sscanf 
    $formatMask = str_replace('{*1*}', '%d', str_replace($placeHolders, '{*1*}', $formatCode));
    // extract the values from the string
    $values = sscanf($string, $formatMask);
    foreach ($values as $k => $v)
    {
      $$phOrder[$k] = $v;
    }
    $result = mktime($hour, $minute, $second, $month, $day, $year);
    return($result);
  }
  
  function stringToDate($string)
  {
    return(stringToDateTime($string, $config['site']['dateformat']));
  }
  
  function stringToTime($string)
  {
    return(stringToDateTime($string, $config['site']['timeformat']));
  }
  
  function inputSafe($raw)
  {
    return(str_replace(
      array('&', '"', '<', '>'),
      array('&amp;', '&quot;', '&lt;', '&gt;'),
      utf8_encode($raw)));
  }

?>
